package net.cmbt.okframe.okbanner;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.AccelerateInterpolator;

import java.lang.reflect.Field;

import static android.view.MotionEvent.ACTION_CANCEL;
import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_OUTSIDE;
import static android.view.MotionEvent.ACTION_UP;

/**
 * Created by LG on 2019/12/10.
 */

public class BaseCoustomViewpager extends ViewPager {
    OKBannerNormal okBannerNormal;
    private float mXp;
    private float mYp;

    public BaseCoustomViewpager(Context context) {
        super(context);
        init();
    }

    public BaseCoustomViewpager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init(){

    }


    public  void setScrollDuration(OKBannerNormal okBannerNormal, int duration){
        this.okBannerNormal=okBannerNormal;
        try {
            Field mField = ViewPager.class.getDeclaredField("mScroller");
            mField.setAccessible(true);
            FixedSpeedScroller mScroller = new FixedSpeedScroller(getContext(),new AccelerateInterpolator());
            mField.set(this, mScroller);
            mScroller.setmDuration(duration);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (getChildCount() == 1) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mXp = ev.getX();
                    mYp = ev.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    return false;

                case MotionEvent.ACTION_UP:
                    return super.dispatchTouchEvent(ev);
            }
            return super.dispatchTouchEvent(ev);

        } else {
            int action = ev.getAction();
            if (action == ACTION_UP || action == ACTION_CANCEL || action == ACTION_OUTSIDE) {
                okBannerNormal.startAutoPlay();
            } else if (action == ACTION_DOWN) {
                okBannerNormal.stopAutoPlay();
            }
            return super.dispatchTouchEvent(ev);
        }
    }





}
